<?php

/**
 * Psan
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category   Psan
 * @package    Psan_Psan
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 * @version    $Id: Results.php 5 2008-04-01 18:27:58Z rutger@maaksite.nl $
 */

/**
 * @see Console_ProgressBar
 */
require_once 'Psan/Psan/Console/ProgressBar.php';

/**
 * @category   Psan
 * @package    Psan_Psan
 * @subpackage Results
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 */
class Psan_Psan_Results
{
    /**
     * Psan Results Generator
     *
     * @param   array   $cacheFiles Files containing 
     * @return  void
     */
    public function __construct($cacheFiles, $url)
    {
        $file = ZEND_ROOT . '/results/' . str_replace('/', '_', $url) . '.html';
        if (file_exists($file)) {
            $file = ZEND_ROOT . '/results/' . str_replace('/', '_', $url) . '_' . time() . '.html';
        }
        $this->_file = fopen($file, 'a+');
        $this->_url = $url;
        
        $this->_fopen = $cacheFiles;
        
        // show progressbar
        $bar = new Console_ProgressBar('[%bar%] %current%/%max%', '=>', ' ', 80, 7);
        ob_flush();
        //do some processing here
        for ($i = 0; $i <= 7; $i++) {
            $bar->update($i);
            ob_flush();
            sleep(1);
        }
        
        $this->_openFile();
        
        // secret stuff
        fputs($this->_file, '<h2>Secret stuff</h2>');
        $this->_parseEmails();
        
        // errors
        fputs($this->_file, '<h2>Errors</h2>');
        $this->_parseCrawlErrors();
        
        $this->_closeFile();
    }
    
    private function _openFile()
    {
        fputs($this->_file, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="nl-nl" xml:lang="nl-nl"><head><title>Psan results for "' . htmlentities($this->_url) . '"</title></head><body><h1>Psan results for "' . htmlentities($this->_url) . '"</h1>');
    }
    
    private function _closeFile()
    {
        fputs($this->_file, '</body></html>');
    }
    
    private function _parseEmails()
    {
        fputs($this->_file, '<h3>Emails</h3>');
        
        $emails = explode("\n", fgets($this->_fopen['emails']));
        if (count($emails) == 1) {
            fputs($this->_file, '<p>We did not found emails</p>');
        } else {
            fputs($this->_file, '<p>We found <strong>' . count($emails) . ' emails</strong>:</p><p><ul><li>' . implode('</li><li>', $emails) . '</li></ul></p>');
        }
    }
    private function _parseCrawlErrors()
    {
        fputs($this->_file, '<h3>Crawl errors</h3>');
        
        $error = '';
        while(!feof($this->_fopen['errors'])) {
            $error .= fgets($this->_fopen['errors'], 4096);
        }
        
        $errors = explode("\n", $error);
        if (count($errors) == 1) {
            fputs($this->_file, '<p>No errors</p>');
        } else {
            fputs($this->_file, '<p><strong>' . count($errors) . ' errors</strong>:</p><p><ul><li>' . implode('</li><li>', $errors) . '</li></ul></p>');
        }
    }
}